import Vue from 'vue'

import app from './App.vue'

//导入vuerouter包
import VueRouter from 'vue-router'
//安装VueRouter
Vue.use(VueRouter)
//导router.js
import route from './router.js'

import moment from 'moment'
Vue.filter('dateFormat',function(dataStr,pattern="YYYY-MM-DD HH:MM:SS"){
    return moment(dataStr).format(pattern)
})
//注册vuex
import Vuex from 'vuex'
Vue.use(Vuex)

//从localStorage加载购物车数据
var car=JSON.parse(localStorage.getItem('car') || '[]')
var store= new Vuex.Store({
    state:{//this.$store.state.***
        car:car//存储购物车信息
    },
    mutations:{//this.store.commit('方法的名称'，唯一参数)
        addToCar(state,goodsinfo){
            var flag=false

            state.car.some(item=>{
                if(item.id==goodsinfo.id){
                    item.count+=parseInt(goodsinfo.count)
                    flag=true
                    return true
                }
            })

            if(!flag){
                state.car.push(goodsinfo)
            }
            //存储在localStorage
            localStorage.setItem('car',JSON.stringify(state.car))
            // localStorage.clear();
        },
        updateGoodsInfo(state,goodsinfo){
            //修改购物车中商品的数量值
            state.car.some(item =>{
                if(item.id == goodsinfo.id){
                    item.count=parseInt(goodsinfo.count);
                    return true;
                }
            })
            //把最新的购物车数据保存到localstore
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        removeFormCar(state,id){
            state.car.some((item,i)=>{
                if(item.id==id){
                    state.car.splice(i,1);
                    return true
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        updateSelected(state,info){
            state.car.forEach(item=>{
                if(item.id==info.id){
                    item.selected=info.selected
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        }
    },
    getters:{//this.$store.getters.***
        getAllCount(state){
            var c=0
            state.car.forEach(item=>{
                c+=item.count
            })
            return c
        },
        getGoodsCount(state){
            var o=[];
            state.car.forEach(item=>{
                o[item.id]=item.count
            })
            return o;
        },
        getGoodsSelected(state){
            var o={}
            state.car.forEach(item => {
                o[item.id]=item.selected
            })
            return o
        },
        getGoodsCountAndAmount(state){
            var o={
                count:0,      //勾选数量
                amount:0        //总价
            }
            state.car.forEach(item=>{
                if(item.selected){
                    o.count+=item.count
                    o.amount+=item.price * item.count
                }
            })
            return o
            
        }
    }
})


//导入vue-resource
import VueResource from 'vue-resource'
//安装
Vue.use(VueResource)
Vue.http.options.root='http://api.cms.liulongbin.top'
Vue.http.options.emulateJSON=true

//选择需要导入mint-ui
// import { Header, Swipe, SwipeItem ,Button,Lazyload} from 'mint-ui'
// Vue.component(Header.name,Header)
// Vue.component(Swipe.name, Swipe)
// Vue.component(SwipeItem.name, SwipeItem)
// Vue.component(Button.name, Button)
// Vue.use(Lazyload);

import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'

//图片预览插件
import VuePreview from 'vue-preview'
Vue.use(VuePreview)

//样式
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'

var vm =new Vue({
    el:"#app",
    render: i=>i(app),
    router:route,
    store
})